$(function(){
    var  commonYear = 2021,commonAreaCode = '330000';
    var areaSelectDatas = [
        { name:'浙江省',value:'330000'},
        { name:'杭州市',value:'330100'},
        { name:'宁波市',value:'330200'},
        { name:'温州市',value:'330300'},
        { name:'嘉兴市',value:'330400'},
        { name:'湖州市',value:'330500'},
        { name:'绍兴市',value:'330600'},
        { name:'金华市',value:'330700'},
        { name:'衢州市',value:'330800'},
        { name:'舟山市',value:'330900'},
        { name:'台州市',value:'331000'},
        { name:'丽水市',value:'331100'},
    ];
    var areaSelects = [];
    layui.use('form', function(){
        var form = layui.form;
        setAreaSelects();
        form.render();
        form.on('select(areaCodeSelect)', function(data){
            commonAreaCode=data.value;
            init()
        });  
    });
    function setAreaSelects(){
        var html = '';
        $.each(areaSelectDatas,function(i,item){
            html += '<option value="'+item.value+'">'+item.name+'</option>'
        })
        $('[lay-filter="areaCodeSelect"]').html(html)
    }
    init();
    function init(){
        getAreaSelects();
        $$.ajaxRequest(ajaxUrl+'/zjax/admin/zjax/ssjTaskTrend',{
            type:"area",
            aab301: commonAreaCode,
            year: commonYear,
        },function(res){
            setLine('#xzjcBarAndLine',{
                "unit":"家",
                "categories": res.data.map(item=>item.name),
                "series":[
                    {"name":"执法检查数","type":"bar","yAxisIndex":0,"data": res.data.map(item=>item.value)},
                ]
            },{
                color:['#15C0FF']
               
            },function(params){
                var index = params.dataIndex;
                // res.data[index].code
                alertSSJTable(params.name,{aab301: res.data[index].code})
            });
        })
        $$.ajaxRequest(ajaxUrl+'/zjax/admin/sbjg/fxsjTrend',{
            aab301: commonAreaCode,
            year: commonYear
        },function(res){
            setLine('#fxxsLine',{
                "unit":["件"],
                "categories": res.data.map(item=>item.name),
                "series":[
                    {"name":"线索数","data": res.data.map(item=>item.value)},
                ]
            },{},function(params){
                var index = params.dataIndex;
                alertFXCZTable(params.name,{ month: res.data[index].code})
            });
        })
        $$.ajaxRequest(ajaxUrl+'/zjax/admin/xyxt/queryXzxkSxZb',{
            xzqh: commonAreaCode,
            year: commonYear
        },function(res){
            setPie('#xzxkzbqkPie',{
                datas: res.data.map(item=>{
                    return {
                        name: item['sxmc'],
                        value: item['fznum'],
                        zhixbm: item.zhixbm,
                        zhuxbm: item.zhuxbm,
                    }
                }),
                unit:"件"
            },{
                // radius:[],
                // center:[],
                legendLeft:'37%',
                showValue:true,
                showPercent:true,
                legendItemGap: 8,
                legendType:'scroll'
            },function(params){//TODO: 事项占比
                alertXZXKTable(params.name,{
                    zhixbm: params.chartData.zhixbm,
                    zhuxbm: params.chartData.zhuxbm
                })
            });
        })
        
        $$.ajaxRequest(ajaxUrl+'/zjax/admin/xyxt/filingAll',{
            xzqh: commonAreaCode,
            year: commonYear
        },function(res){
            setBarOrLine('#xzxkbjsbBar',{
                "unit":["件","%"],
                "categories": res.data.map(item=>item['area_name']),
                "series":[
                    {"name":"办件量","type":"bar","yAxisIndex":0,"data": res.data.map(item=>item['total'])},
                    {"name":"告知承诺件","type":"bar","yAxisIndex":0,"data": res.data.map(item=>item['gzcn'])},
                    {"name":"撤销行政许可","type":"bar","yAxisIndex":0,"data": res.data.map(item=>item['qxzsyxx'])},
                ]
            },{
                twoYAxis:false,
                xAxisRotate: 45,
                barWidth: $$.fitFontSize(10)
            },function(params){//TODO: 各地办件申报情况
                var index = params.dataIndex;
                var type = params.seriesIndex;
                var dataParams = {
                    xzqh: res.data[index].area_code,
                }
                if(type == 1){
                    dataParams.gzcnbz = 1;
                }else if(type == 2){
                    dataParams.qxbz = 1;
                }
                alertXZXKTable(params.seriesName+'-'+params.name,dataParams)
            });
        })
        
        $$.ajaxRequest(ajaxUrl+'/zjax/admin/xyxt/bjlx',{
            xzqh: commonAreaCode,
            year: commonYear
        },function(res){
            setPie('#xzxkbjlxPie',{
                datas: res.data.map(item=>{
                    return {
                        name: item['lx'],
                        value: item['num']
                        //PROMISE: "2",
                    }
                }),
                unit:"件"
            },{
                showValue:true,
                showPercent:true,
                legendItemGap:20
            },function(params){//TODO: 类型
                alertXZXKTable(params.name,{gzcnbz: res.data[params.dataIndex].promise})
            });
        })  
    }
    function getAreaSelects(){
        $$.ajaxRequest(ajaxUrl+'/zjax/admin/zjax/getAab301Tree',{},function(res){
            if(res.data && res.data.length>0){
                areaSelects = [{
                    id: "330000",
                    title: "浙江省",
                    child: getChildren('330000',res.data)
                }]
            }
            function getChildren(areacode,data){
                var child = [];
                $.each(data,function(i,item){
                    if(item.pid == areacode){
                        child.push({
                            id: item.id,
                            pid: item.pid||'',
                            title: item.text,
                            child: getChildren(item.id,data)
                        });
                    }
                })
                return child;
            }
        })
    }
    function alertSSJTable(title,params){//双随机检查
        $$.alertTable({
            title: title||'双随机检查',
            colsData: [[
                { title: '序号', type: 'numbers' },
                // {title: "任务来源名称", field: "tasksourcename"},
			    // {title: "检查对象名称", field: "legalentityname"},
				{title: "统一社会信用代码", field: "unicode"},
				{title: "任务编码", field: "taskcode"},
				{title: "创建时间", field: "createtime"},
				{title: "检查完成时间", field: "checkcompletetime"},
				{title: "检查人员", field: "supervisorname"},
				{title: "检查部门", field: "deptname"}
            ]],
            url: ajaxUrl + '/zjax/admin/zjax/ssjTaskList',
            where: Object.assign({
                aab301: commonAreaCode,
                year: commonYear
            },params),
            pageProp: {
                limit: 10
            },
            searchColumn:[
                    {name: "所属管辖", field: "aab301",type:'dropdown',dicData: areaSelects},
                    {name: "检查对象名称", field: "legalEntityname", type: "text"},
                    {name: "创建时间", field: "create",type:"datePicker",range:true,subtype: "date",format: 'yyyy年MM月dd日'},
            ],
            rowClickFun: function (obj) {// obj.data
                alertSSJDetails(obj);
            }
        });
    }
    function alertSSJDetails(obj){
        var tableColumns = [
            [
                {title: "任务类型", field: "tasktype"},
                // {title: "任务来源名称", field: "tasksourcename"},
                {title: "任务编码", field: "taskcode"},
                {title: "创建时间", field: "createtime"},
                {title: "计划完成时间", field: "plancompletetime"},
                {title: "检查完成时间", field: "checkcompletetime"},
                {title: "检查对象名称", field: "legalentityname"},
                {title: "统一社会信用代码", field: "unicode"},
                {title: "检查部门", field: "deptname"},
                {title: "部门编码", field: "deptuniquecode"},
                {title: "查无下落原因", field: "checknowhereaboutsreason"},
                {title: "任务状态", field: "taskstatus"},
                {title: "任务录入人", field: "setusername"}
            ]
        ];
        var ecm = $$.alert('yj');
        $('#' + ecm).html(`
            <div class="title">详情</div>
            <div class="table_con">
                <div class="model">
                    <div class="model_title">基本信息</div>
                    <div class="model_content ztxxTable"></div>
                </div>
            </div>
        `);
        
        $$.ajaxRequest(ajaxUrl + '/zjax/admin/zjax/ssjTaskDetail',{
            taskid: obj.data.taskid
        },function(res){
            var data = res.data ||　{};
            $$.title2Table('#'+ecm+' .ztxxTable',tableColumns[0], data||{},2);
        });
    }
    function alertFXCZTable(title,params){//风险处置
        $$.alertTable({
            title: title||'风险处置',
            colsData: [[
                { title: '序号', type: 'numbers' },
                {title: "风险时间", field: "discover_time"},
                {title: "风险来源系统", field: "event_classify_content"},
                {title: "涉及单位", field: "event_entity_name"},
                {title: "风险标题", field: "event_title"},
                {title: "风险处置部门", field: "disposal_org_name"}
            ]],
            url: ajaxUrl + '/zjax/admin/zjax/fxczList',
            where: Object.assign({
                aab301: commonAreaCode,
                year: commonYear
            },params),
            pageProp: {
                limit: 10
            },
            searchColumn:[
                    {name: "所属管辖", field: "aab301",type:'dropdown',dicData: areaSelects},
                    {name: "单位名称", field: "entityname", type: "text"},
            ],
            rowClickFun: function (obj) {// obj.data
                var ecm = $$.alert('yj');
                $('#' + ecm).html(`
                    <div class="title">详情</div>
                    <div class="table_con">
                        <div class="model">
                            <div class="model_title">基本信息</div>
                            <div class="model_content ztxxTable"></div>
                        </div>
                    </div>
                `);
                
                $$.ajaxRequest(ajaxUrl + '/zjax/admin/zjax/fxczDetail',{
                    uid: obj.data.uid
                },function(res){
                    var data = res.data ||　{};
                    $$.title2Table('#'+ecm+' .ztxxTable',[
                        {title: "风险来源系统", field: "event_classify_content"},
                        {title: "风险标题", field: "event_title"},
                        {title: "风险时间", field: "discover_time"},
                        {title: "涉及单位", field: "event_entity_name"},
                        {title: "风险描述", field: "event_desc",fitWidth:true},
                        {title: "处理状态", field: "event_status"},
                        {title: "风险处置部门", field: "disposal_org_name"},
                        {title: "处置说明", field: "process_directions",fitWidth:true}
                    ], data||{},2);
                });
            }
        });
    }
    function alertXZXKTable(title,params){//TODO: 行政许可
        $$.alertTable({
            title: title||'行政许可',
            colsData: [[
                { title: '序号', type: 'numbers' },
                {field: "apply_cardnumber", title: "证件号"},
                // {field: "apply_cardtype", title: "证件类型编码"},
                {field: "apply_cardtype_text", title: "证件类型"},
                {field: "applyname", title: "申报主体"},
                // {field: "bac1", title: "-"},
                // {field: "bjid", title: "办件id"},
                // {field: "bjjg", title: "办结结果编码"},
                {field: "bjjg_text", title: "办结结果"},
                // {field: "bjly", title: "办件来源"},
                {field: "bjly_text", title: "办件来源"},
                {field: "bjmc", title: "办件名称"},
                {field: "bjsj", title: "办结时间"},
                {field: "is_promise_text", title: "是否是告知承诺"},
                {field: "qlsxbbh", title: "权力事项版本号"},
                {field: "qlsxbm", title: "权力事项编码"},
                {field: "qlsxmc", title: "权力事项名称"},
                // {field: "rownum_", title: "-"},
                // {field: "sbbh", title: "-"},
                {field: "sbh", title: "申报号"},
                {field: "sbsj", title: "申报时间"},
                // {field: "sjrid", title: "-"},
                {field: "slsj", title: "受理时间"},
                // {field: "status", title: "-"},
                // {field: "sxid", title: "事项id"},
                // {field: "sxlx", title: "事项类型"},
                {field: "sxlx_text", title: "事项类型"},
                // {field: "zbcs", title: "-"},
                // {field: "zhixbm", title: "子项编码"},
                // {field: "zhuxbm", title: "主项编码"}
            ]],
            url: ajaxUrl + '/zjax/admin/xyxt/queryXzxkList',
            where: Object.assign({
                aab301: commonAreaCode,
                year: commonYear
            },params),
            searchColumn:[],
            rowClickFun: function (obj) {// obj.data
                var ecm = $$.alert('yj');
                $('#' + ecm).html(`
                    <div class="title">详情</div>
                    <div class="table_con">
                        <div class="model">
                            <div class="model_title">基本信息</div>
                            <div class="model_content ztxxTable"></div>
                        </div>
                    </div>
                `);
                
                $$.ajaxRequest(ajaxUrl + '/zjax/admin/xyxt/queryBjByBjId',{
                    bjId: obj.data.bjid
                },function(res){
                    var data = res.data ||　{};
                    $$.title2Table('#'+ecm+' .ztxxTable',[
                        {field: "applyname", title: "申报主体",fitWidth:true},
                        {field: "apply_cardnumber", title: "证件号"},
                        {field: "apply_cardtype_text", title: "证件类型"},
                        {field: "bjmc", title: "办件名称",fitWidth:true},
                        {field: "bjjg_text", title: "办结结果"},
                        {field: "bjly_text", title: "办件来源"},
                        {field: "bjsj", title: "办结时间"},
                        {field: "is_promise_text", title: "是否是告知承诺"},
                        // {field: "qlsxbbh", title: "权力事项版本号"},
                        // {field: "qlsxbm", title: "权力事项编码"},
                        {field: "qlsxmc", title: "权力事项名称",fitWidth:true},
                        {field: "sbsj", title: "申报时间"},
                        {field: "slsj", title: "受理时间"},
                        {field: "sxlx_text", title: "事项类型"},
                    ], data||{},2);
                });
            }
        });
    }
})
   